SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE FUNCTION [dbo].[FU_FECH_PAGO] 
/*------------------------------------------------------*/      
/*---- Empresa           : OFISIS S.A.              ----*/      
/*---- Cliente           : OFISIS S.A.              ----*/      
/*---- Nombre FU         : FU_FECH_PAGO		    ----*/      
/*---- Desarrollado por  : Daniel Chiang 	    ----*/      
/*---- Fecha Creación    : 09/03/2003               ----*/      
/*---- Base de Datos     : SLQ Server               ----*/      
/*---- Versión           : 8.0                      ----*/      
/*------------------------------------------------------*/      
/*---Modificado 1 por     :                         ----*/    
/*---Fecha Modificación   :                         ----*/    
/*---Detalle Modificación :                         ----*/    
/*------------------------------------------------------*/      
/*---  Declare  @PRUEBA TD_NU_016_004    
       Select @PRUEBA = (SELECT dbo.FU_FECH_PAGO('EUR', 'SOL', 1000, 'SOL', 'DOL', 3.50, 1.2))    
       Select @PRUEBA       ---*/    
/*------------------------------------------------------*/      
(    
@ISCO_EMPR VarChar(2),    
@ISCO_PROV VarChar(20),    
@ISTI_DOCU VarChar(3),    
@ISNU_DOCU VarChar(20),    
@IDFE_VENC Datetime    
)    
    
Returns  Datetime    
    
As    
Begin    
  Declare    
  @VDFE_OPER Datetime,    
  @VDFE_PAGO Datetime    
     
        Select @VDFE_PAGO = Null    
    
 Select @VDFE_OPER = T3.FE_OPER    
        From    
 (    
 Select T2.FE_OPER FE_OPER    
 From   TDMOVI_BANC T1, TCMOVI_BANC T2    
 Where  T1.CO_EMPR = @ISCO_EMPR    
 And    T1.TI_ENTI = 'P'    
 AND    T1.CO_ENTI = @ISCO_PROV    
 AND    T1.TI_DOCU_ENTI = @ISTI_DOCU    
 AND    T1.NU_DOCU_ENTI = @ISNU_DOCU    
 And    T2.TI_SITU = 'ACT'     
 And    T1.CO_EMPR = T2.CO_EMPR      
 And    T1.CO_BANC = T2.CO_BANC      
 And    T1.NU_CNTA_BANC  = T2.NU_CNTA_BANC      
 And    T1.AA_BNCO = T2.AA_BNCO    
 And    T1.MM_BNCO = T2.MM_BNCO     
 And    T1.TI_MOVI_BANC = T2.TI_MOVI_BANC     
 And    T1.NU_COMP_BANC = T2.NU_COMP_BANC    
 Union All    
 Select T2.FE_OPER    
 From   TDMOVI_CAJA T1, TCMOVI_CAJA T2    
 Where  T1.CO_EMPR = @ISCO_EMPR    
 And    T1.TI_ENTI = 'P'    
 AND    T1.CO_ENTI = @ISCO_PROV    
 AND    T1.TI_DOCU_ENTI = @ISTI_DOCU    
 AND    T1.NU_DOCU_ENTI = @ISNU_DOCU    
 And    T2.TI_SITU = 'ACT'     
 And    T1.CO_EMPR = T2.CO_EMPR      
 And    T1.CO_CAJA = T2.CO_CAJA    
 And    T1.AA_CAJA = T2.AA_CAJA    
 And    T1.MM_CAJA = T2.MM_CAJA     
 And    T1.TI_MOVI = T2.TI_MOVI    
 And    T1.NU_COMP_CAJA = T2.NU_COMP_CAJA) T3    
    
        If @VDFE_OPER IS NOT NULL    
           If @VDFE_OPER < @IDFE_VENC    
              Select @VDFE_PAGO = @VDFE_OPER    
           Else    
              Select @VDFE_PAGO = @IDFE_VENC    
        Else    
              Select @VDFE_PAGO = @IDFE_VENC     
    
Return @VDFE_PAGO    

End    

/*------------------------ FIN ----------------------------*/

GO
